<?php
declare(strict_types=1);
// 货商档案
namespace app\admin_v1\controller;
use think\facade\Db; //门面模式
use app\admin_v1\model\Sup as mysql; //导入模型
class Sup
{
    // 新增
    public function new()
    {
        //本API说明
        $api_info = [
            '本API说明' => [
                "本API名称" => "新增一条货商档案信息",
                "本API版号" => "1.001",
                "本API更新" => "2023-11-25",
                "本API地址" => request()->domain() . request()->baseUrl(),
                "本API当前请求类型" => request()->method(),
                "本API建议请求类型" => "POST",
                "本API要求的请求参数" => [
                    "main" => "主体部分",
                    "all_sup_addr" => "货商其它地址",
                    "all_sup_bank" => "货商其它银行账号",
                    "main (货商主体数据)可改写字段" => [
                        "name" =>  "货商名称:字符串", //货商名称
                        "principal" =>  "默认负责人:字符串", //默认负责人
                        "tel" =>  "默认电话:字符串", //默认电话
                        "mobile" =>  "默认手机:字符串", //默认手机
                        "fax" =>  "默认传真:字符串", //默认传真
                        "email" =>  "默认电子邮箱:字符串", //默认电子邮箱
                        "bank_acctno" =>  "默认开户银行:字符串", //默认开户银行
                        "bank_no" =>  "默认银行账号:字符串", //默认银行账号
                        "addr" =>  "默认地址:字符串", //默认地址
                        "memo" =>  "备注:字符串", //备注
                    ],
                    "all_sup_addr (货商其它地址数组包)可改写字段" => [
                        "name" => "名称",
                        "principal" => "负责人",
                        "tel" => "联系电话",
                        "mobile" => "手机",
                        "fax" => "传真",
                        "email" => "默认电子邮箱",
                        "addr" => "地址",
                        "memo" => "备注"
                    ],
                    "all_sup_bank (货商其它银行账号数组包)可改写字段" => [
                        "name" => "名称",
                        "bank_acctno" => "开户银行",
                        "bank_no" => "银行账号",
                        "memo" => "备注"
                    ],
                ],
                "本API简介" => "提交要有3个对象包:{main:object,all_sup_addr:array,all_sup_bank:array}",
                "请求回调字段说明" => [
                    "code" => "状态码:整数(200=正常/201=写失败/202=请求参数有误)",
                    "msg" => "状态信息:字符串",
                    "query" => "您发出的请求参数",
                    "db" => "新增的数据",
                ]
            ],
        ];
        $time_int = time();
        $time = date('Y-m-d H:i:s', $time_int);
        $query = request()->param(); //请求参数
        // 请求方式验证
        if (!request()->isPost() || empty($query["main"]) || empty($query["all_sup_addr"]) || empty($query["all_sup_bank"])) {
            $apidb = [
                "api_info" => $api_info, //API信息
                "code" => 202, //状态码
                "msg" => "请求有误-要求Post方式、及完善参数", //状态信息
                "query" => $query, //请求参数
                "db" => [],
                "script_time" => date('Y-m-d H:i:s', time()), //脚本时间
            ];
            return json($apidb);
        };
        $dbid = 'SUP-' . common__guid()["salt_any"];
        $sqldb = mysql::create(
            [
                'dbid' => $dbid, //数据编号.唯一
                "name" => (isset($query["main"]["name"]) && !empty($query["main"]["name"])) ? $query["main"]["name"] : NULL, //货商名称:字符串
                "principal" => (isset($query["main"]["principal"]) && !empty($query["main"]["principal"])) ? $query["main"]["principal"] : NULL, //默认负责人:字符串
                "tel" => (isset($query["main"]["tel"]) && !empty($query["main"]["tel"])) ? $query["main"]["tel"] : NULL, //默认电话:字符串
                "mobile" => (isset($query["main"]["mobile"]) && !empty($query["main"]["mobile"])) ? $query["main"]["mobile"] : NULL, //默认手机:字符串
                "fax" => (isset($query["main"]["fax"]) && !empty($query["main"]["fax"])) ? $query["main"]["fax"] : NULL, //默认传真:字符串
                "email" => (isset($query["main"]["email"]) && !empty($query["main"]["email"])) ? $query["main"]["email"] : NULL, //默认电子邮箱:字符串
                "bank_acctno" => (isset($query["main"]["bank_acctno"]) && !empty($query["main"]["bank_acctno"])) ? $query["main"]["bank_acctno"] : NULL, //默认开户银行:字符串
                "bank_no" => (isset($query["main"]["bank_no"]) && !empty($query["main"]["bank_no"])) ? $query["main"]["bank_no"] : NULL, //默认银行账号:字符串
                "addr" => (isset($query["main"]["addr"]) && !empty($query["main"]["addr"])) ? $query["main"]["addr"] : NULL, //默认地址:字符串
                "memo" => (isset($query["main"]["memo"]) && !empty($query["main"]["memo"])) ? $query["main"]["memo"] : NULL, //备注:字符串"
                'time_new' => $time_int, //创建时间
                'time_up' => $time_int, //修改时间
                // 'db_json' => json_encode($query, JSON_UNESCAPED_UNICODE), //静态JSON
            ]
        );
        // 写入-成功
        if ($sqldb) {
            $code = 200;
            $msg = "新增-成功";
            if ($sqldb["dbid"]) {
                $RS1 = $query["all_sup_addr"]; //货商其它地址
                if (count($RS1) > 0) {
                    for ($i = 0; $i < count($RS1); $i++) {
                        $insert_all_sup_addr[$i]["sup_dbid"] = $sqldb["dbid"]; //归属货商数据编号
                        $insert_all_sup_addr[$i]["name"] = isset($RS1[$i]["name"]) ? $RS1[$i]["name"] : null; //地址名称
                        $insert_all_sup_addr[$i]["principal"] = isset($RS1[$i]["principal"]) ? $RS1[$i]["principal"] : null; //负责人
                        $insert_all_sup_addr[$i]["tel"] = isset($RS1[$i]["tel"]) ? $RS1[$i]["tel"] : null; //联系电话
                        $insert_all_sup_addr[$i]["mobile"] = isset($RS1[$i]["mobile"]) ? $RS1[$i]["mobile"] : null; //手机
                        $insert_all_sup_addr[$i]["fax"] = isset($RS1[$i]["fax"]) ? $RS1[$i]["fax"] : null; //传真
                        $insert_all_sup_addr[$i]["email"] = isset($RS1[$i]["email"]) ? $RS1[$i]["email"] : null; //电子邮箱
                        $insert_all_sup_addr[$i]["addr"] = isset($RS1[$i]["addr"]) ? $RS1[$i]["addr"] : null; //地址
                        $insert_all_sup_addr[$i]["memo"] = isset($RS1[$i]["memo"]) ? $RS1[$i]["memo"] : null; //备注
                    }
                    //分批写入每次最多100条数据
                    $sql_insert_all_sup_addr = Db::name('admin__i_sup_addr')->strict(false)->limit(100)->insertAll($insert_all_sup_addr);
                }
                $RS2 = $query["all_sup_bank"]; //货商其它银行账号
                if (count($RS2) > 0) {
                    for ($i = 0; $i < count($RS2); $i++) {
                        $insert_all_sup_bank[$i]["sup_dbid"] = $sqldb["dbid"]; //归属货商数据编号
                        $insert_all_sup_bank[$i]["name"] = isset($RS2[$i]["name"]) ? $RS2[$i]["name"] : null; //银行名称
                        $insert_all_sup_bank[$i]["bank_acctno"] = isset($RS2[$i]["bank_acctno"]) ? $RS2[$i]["bank_acctno"] : null; //开户银行
                        $insert_all_sup_bank[$i]["bank_no"] = isset($RS2[$i]["bank_no"]) ? $RS2[$i]["bank_no"] : null; //银行账号
                        $insert_all_sup_bank[$i]["memo"] = isset($RS2[$i]["memo"]) ? $RS2[$i]["memo"] : null; //备注
                    }
                    //分批写入每次最多100条数据
                    $sql_insert_all_sup_bank = Db::name('admin__i_sup_bank')->strict(false)->limit(100)->insertAll($insert_all_sup_bank);
                }
            }
        } else {
            $code = 201;
            $msg = "新增-失败";
        }
        $apidb = [
            "api_info" => $api_info, //API信息
            "code" => $code, //状态码
            "msg" => $msg, //状态信息
            "query" => $query, //请求参数
            "db" => $sqldb,
            "script_time" => date('Y-m-d H:i:s', time()), //脚本时间
        ];
        return (json($apidb));
    }
    // 修改
    public function up()
    {
        //本API说明
        $api_info = [
            '本API说明' => [
                "本API名称" => "修改某一货商档案信息",
                "本API版号" => "1.001",
                "本API更新" => "2023-11-25",
                "本API地址" => request()->domain() . request()->baseUrl(),
                "本API当前请求类型" => request()->method(),
                "本API建议请求类型" => "POST",
                "本API要求的请求参数" => [
                    "main" => "主体部分",
                    "all_sup_addr" => "货商其它地址",
                    "all_sup_bank" => "货商其它银行账号",
                    "main (货商主体数据)条件与可改写字段" => [
                        "dbid" =>  "[必填-条件字段]货商的数据编号:字符串", //货商的数据编号
                        "name" =>  "货商名称:字符串", //货商名称
                        "principal" =>  "默认负责人:字符串", //默认负责人
                        "tel" =>  "默认电话:字符串", //默认电话
                        "mobile" =>  "默认手机:字符串", //默认手机
                        "fax" =>  "默认传真:字符串", //默认传真
                        "email" =>  "默认电子邮箱:字符串", //默认电子邮箱
                        "bank_acctno" =>  "默认开户银行:字符串", //默认开户银行
                        "bank_no" =>  "默认银行账号:字符串", //默认银行账号
                        "addr" =>  "默认地址:字符串", //默认地址
                        "memo" =>  "备注:字符串", //备注
                    ],
                    "all_sup_addr (货商其它地址数组包)可改写字段" => [
                        "name" => "名称",
                        "principal" => "负责人",
                        "tel" => "联系电话",
                        "mobile" => "手机",
                        "fax" => "传真",
                        "email" => "默认电子邮箱",
                        "addr" => "地址",
                        "memo" => "备注"
                    ],
                    "all_sup_bank (货商其它银行账号数组包)可改写字段" => [
                        "name" => "名称",
                        "bank_acctno" => "开户银行",
                        "bank_no" => "银行账号",
                        "memo" => "备注"
                    ],
                ],
                "本API简介" => "提交要有3个对象包:{main:object,all_sup_addr:array,all_sup_bank:array}",
                "请求回调字段说明" => [
                    "code" => "状态码:整数(200=正常/201=查询不存在)",
                    "msg" => "状态信息:字符串",
                    "query" => "您发出的请求参数",
                    // 'db_old' => "旧数据集", //旧数据集
                    // 'db_new' => "新数据集", //新数据集
                ]
            ],
        ];
        $time_int = time();
        $time = date('Y-m-d H:i:s', $time_int);
        $query = request()->param(); //请求参数
        // 查询一条
        $sqldb = mysql::where('dbid', '=', $query["main"]["dbid"])->findOrEmpty();
        if ($sqldb->isEmpty()) {
            //数据集为空
            $isEmpty = true;
        } else {
            $isEmpty = false;
        }
        if ($isEmpty) {
            $msg = "查询失败-对应货商不存在!".$query["main"]["dbid"];
            $code = 201;
            $db_old = []; //旧数据集
        } else {
            $msg = "查询成功!";
            $code = 200;
            $db_old = $sqldb->toArray(); //旧数据集
            $sqldb->name = (isset($query["main"]["name"]) && !empty($query["main"]["name"])) ? $query["main"]["name"] : $db_old["name"]; //货商名称:字符串
            $sqldb->principal = (isset($query["main"]["principal"]) && !empty($query["main"]["principal"])) ? $query["main"]["principal"] : $db_old["principal"]; //默认负责人:字符串
            $sqldb->tel = (isset($query["main"]["tel"]) && !empty($query["main"]["tel"])) ? $query["main"]["tel"] : $db_old["tel"]; //默认电话:字符串
            $sqldb->mobile = (isset($query["main"]["mobile"]) && !empty($query["main"]["mobile"])) ? $query["main"]["mobile"] : $db_old["mobile"]; //默认手机:字符串
            $sqldb->fax = (isset($query["main"]["fax"]) && !empty($query["main"]["fax"])) ? $query["main"]["fax"] : $db_old["fax"]; //默认传真:字符串
            $sqldb->email = (isset($query["main"]["email"]) && !empty($query["main"]["email"])) ? $query["main"]["email"] : $db_old["email"]; //默认电子邮箱:字符串
            $sqldb->bank_acctno = (isset($query["main"]["bank_acctno"]) && !empty($query["main"]["bank_acctno"])) ? $query["main"]["bank_acctno"] : $db_old["bank_acctno"]; //默认开户银行:字符串
            $sqldb->bank_no = (isset($query["main"]["bank_no"]) && !empty($query["main"]["bank_no"])) ? $query["main"]["bank_no"] : $db_old["bank_no"]; //默认银行账号:字符串
            $sqldb->addr = (isset($query["main"]["addr"]) && !empty($query["main"]["addr"])) ? $query["main"]["addr"] : $db_old["addr"]; //默认地址:字符串
            $sqldb->memo = (isset($query["main"]["memo"]) && !empty($query["main"]["memo"])) ? $query["main"]["memo"] : $db_old["memo"]; //备注:字符串"
            $sqldb->time_up = $time_int; //修改时间
            $sqldb->save();
            // "all_sup_addr": "货商其它地址
            // "all_sup_bank": "货商其它银行账号
            // 货商其它地址-先删除再新增
            $all_sup_addr_DEL =   Db::table('admin__i_sup_addr')->where('sup_dbid', '=', $sqldb["dbid"])->delete(); //删除
            if ($all_sup_addr_DEL) {
                $RS1 = $query["all_sup_addr"]; //货商其它地址
                if (count($RS1) > 0) {
                    for ($i = 0; $i < count($RS1); $i++) {
                        $insert_all_sup_addr[$i]["sup_dbid"] = $sqldb["dbid"]; //归属货商数据编号
                        $insert_all_sup_addr[$i]["name"] = isset($RS1[$i]["name"]) ? $RS1[$i]["name"] : null; //地址名称
                        $insert_all_sup_addr[$i]["principal"] = isset($RS1[$i]["principal"]) ? $RS1[$i]["principal"] : null; //负责人
                        $insert_all_sup_addr[$i]["tel"] = isset($RS1[$i]["tel"]) ? $RS1[$i]["tel"] : null; //联系电话
                        $insert_all_sup_addr[$i]["mobile"] = isset($RS1[$i]["mobile"]) ? $RS1[$i]["mobile"] : null; //手机
                        $insert_all_sup_addr[$i]["fax"] = isset($RS1[$i]["fax"]) ? $RS1[$i]["fax"] : null; //传真
                        $insert_all_sup_addr[$i]["email"] = isset($RS1[$i]["email"]) ? $RS1[$i]["email"] : null; //电子邮箱
                        $insert_all_sup_addr[$i]["addr"] = isset($RS1[$i]["addr"]) ? $RS1[$i]["addr"] : null; //地址
                        $insert_all_sup_addr[$i]["memo"] = isset($RS1[$i]["memo"]) ? $RS1[$i]["memo"] : null; //备注
                    }
                    //分批写入每次最多100条数据
                    $sql_insert_all_sup_addr = Db::name('admin__i_sup_addr')->strict(false)->limit(100)->insertAll($insert_all_sup_addr);
                }
            }
            // 货商其它银行账号-先删除再新增
            $all_sup_bank_DEL =   Db::table('admin__i_sup_bank')->where('sup_dbid', '=', $sqldb["dbid"])->delete(); //删除
            if ($all_sup_bank_DEL) {
                $RS2 = $query["all_sup_bank"]; //货商其它银行账号
                if (count($RS2) > 0) {
                    for ($i = 0; $i < count($RS2); $i++) {
                        $insert_all_sup_bank[$i]["sup_dbid"] = $sqldb["dbid"]; //归属货商数据编号
                        $insert_all_sup_bank[$i]["name"] = isset($RS2[$i]["name"]) ? $RS2[$i]["name"] : null; //银行名称
                        $insert_all_sup_bank[$i]["bank_acctno"] = isset($RS2[$i]["bank_acctno"]) ? $RS2[$i]["bank_acctno"] : null; //开户银行
                        $insert_all_sup_bank[$i]["bank_no"] = isset($RS2[$i]["bank_no"]) ? $RS2[$i]["bank_no"] : null; //银行账号
                        $insert_all_sup_bank[$i]["memo"] = isset($RS2[$i]["memo"]) ? $RS2[$i]["memo"] : null; //备注
                    }
                    //分批写入每次最多100条数据
                    $sql_insert_all_sup_bank = Db::name('admin__i_sup_bank')->strict(false)->limit(100)->insertAll($insert_all_sup_bank);
                }
            }
        }
        $apidb = [
            "api_info" => $api_info, //API信息
            "code" => $code, //状态码
            "msg" => $msg, //状态信息
            "query" => $query, //请求参数
            // "db_old" => $db_old, //旧数据集
            // "db_new" => $sqldb, //新数据集
            "script_time" => date('Y-m-d H:i:s', time()), //脚本时间
        ];
        return (json($apidb));
    }
    // 找到一条
    public function id()
    {
        $query = request()->param(); //请求参数
        if (isset($query["dbid"])) {
            $sqldb = mysql::where('dbid', '=', $query["dbid"])->findOrEmpty(); //精准查询
        } else {
            $sqldb = mysql::order("id", "desc")->findOrEmpty(); //空查返回最新一条数据
        };
        if ($sqldb->isEmpty()) {
            //数据集为空
            $isEmpty = true;
        } else {
            $isEmpty = false;
        }
        if ($isEmpty) {
            $msg = '查询失败-对应货商不存在!';
            $code = 201;
        } else {
            $msg = '查询成功!';
            $code = 200;
            // 设置需要附加的输出属性
            $sqldb = $sqldb->append([
                'all_sup_addr', //附加字段-货商其它地址
                'all_sup_bank', //附加字段-货商其它银行账号
            ]);
            // 设置数据字段获取器
            $sqldb = $sqldb->withAttr('time_new', function ($value, $data) {
                return  ["val" => $value, "mp" => date('Y-m-d H:i:s', $value), "txt" => "创建时间"];
            });
            $sqldb = $sqldb->withAttr('time_up', function ($value, $data) {
                return  ["val" => $value, "mp" => date('Y-m-d H:i:s', $value), "txt" => "修改时间"];
            });
            // 附加字段-货商其它地址
            $sqldb["all_sup_addr"] =  Db::table('admin__i_sup_addr')->where('sup_dbid', $sqldb['dbid'])->select();
            // 附加字段-货商其它银行账号
            $sqldb["all_sup_bank"] =  Db::table('admin__i_sup_bank')->where('sup_dbid', $sqldb['dbid'])->select();
        }
        //本API说明
        $api_info = [
            '本API说明' => [
                "本API名称" => "查询某一货商档案信息",
                "本API版号" => "1.001",
                "本API更新" => "2023-11-25",
                "本API地址" => request()->domain() . request()->baseUrl(),
                "本API当前请求类型" => request()->method(),
                "本API建议请求类型" => "POST",
                "本API要求的请求参数" => [
                    "dbid" => "货商的数据编号",
                ],
                "本API简介" => "提交有dbid字段精准返回数据,否则忽略一切返回最新一条数据。",
                "请求回调字段说明" => [
                    "code" => "状态码:整数(200=正常/201=查询不存在)",
                    "msg" => "状态信息:字符串",
                    "query" => "您发出的请求参数",
                    "db 数据集字段说明"  => [
                        "id" =>  "主键:整数", //主键
                        "time_new" =>  "创建时间:对象{val,mp,txt}", //创建时间
                        "time_up" =>  "修改时间:对象{val,mp,txt}", //修改时间
                        "db_json" => "静态JSON", //静态JSON
                        "dbid" =>  "数据编号.唯一:字符串", //数据编号.唯一
                        "name" =>  "货商名称:字符串", //货商名称
                        "principal" =>  "默认负责人:字符串", //默认负责人
                        "tel" =>  "默认电话:字符串", //默认电话
                        "mobile" =>  "默认手机:字符串", //默认手机
                        "fax" =>  "默认传真:字符串", //默认传真
                        "email" =>  "默认电子邮箱:字符串", //默认电子邮箱
                        "bank_acctno" =>  "默认开户银行:字符串", //默认开户银行
                        "bank_no" =>  "默认银行账号:字符串", //默认银行账号
                        "addr" =>  "默认地址:字符串", //默认地址
                        "memo" =>  "备注:字符串", //备注
                        "all_sup_addr" => "附加字段-货商其它地址", //附加字段-货商其它地址
                        "all_sup_bank" => "附加字段-货商其它银行账号", //附加字段-货商其它银行账号
                        "all_sup_addr 数据集字段说明"  => [
                            "id" => "主键:整数",
                            "sup_dbid" => "归属货商数据编号",
                            "name" => "名称",
                            "principal" => "负责人",
                            "tel" => "联系电话",
                            "mobile" => "手机",
                            "fax" => "传真",
                            "email" => "默认电子邮箱",
                            "addr" => "地址",
                            "memo" => "备注"
                        ],
                        "all_sup_bank 数据集字段说明"  => [
                            "id" => "主键:整数",
                            "sup_dbid" => "归属货商数据编号",
                            "name" => "名称",
                            "bank_acctno" => "开户银行",
                            "bank_no" => "银行账号",
                            "memo" => "备注"
                        ],
                    ]
                ]
            ],
        ];
        $apidb = [
            'api_info' => $api_info, //API信息
            'code' => $code, //状态码
            'msg' => $msg, //状态信息
            'query' => $query, //请求参数
            'db' => $sqldb,
            "script_time" => date('Y-m-d H:i:s', time()), //脚本时间
        ];
        return (json($apidb));
    }
    // 列表
    public function List()
    {
        $time_int = time();
        $time = date('Y-m-d H:i:s', $time_int);
        $query = request()->param(); //请求参数
        $limit = request()->param('limit', 10, 'trim'); //每页行数
        $current = request()->param('current', 1, 'trim'); //页码
        if (isset($query["additional"]["use"]) == 1) {
            // additional.use启用额外查询方法-0否-1是。
            $SearchField = isset($query["additional"]["SearchField"]) ? $query["additional"]["SearchField"] : "id"; //搜索字段
            $SearchOperator = isset($query["additional"]["SearchOperator"]) ? $query["additional"]["SearchOperator"] : "LIKE"; //搜索查询表达式
            $SearchaAtistop = isset($query["additional"]["SearchaAtistop"]) ? $query["additional"]["SearchaAtistop"] : ""; //搜索关键词SearchaAtistop:"[4,28,29,30,31,32,33,34]"
        } else {
            $SearchField = request()->param('SearchField', 'id', 'trim'); //搜索字段
            $SearchOperator = request()->param('SearchOperator', 'LIKE', 'trim'); //搜索查询表达式
            $SearchaAtistop = strval(request()->param('SearchaAtistop', "", 'trim')); //搜索关键词
        }
        $SearchaSort = request()->param('SearchaSort', 'DESC', 'trim'); //搜索排序方式ASE,DESC升降序
        $SearchaSortField = request()->param('SearchaSortField', 'id', 'trim'); //排序字段
        // strval();//转成字符串
        // echo gettype(); //打印变量的类型
        // 查询表达式
        if ($SearchOperator == 'LIKE') {
            $SearchaAtistop = '%' . $SearchaAtistop . '%';
        };
        if ($SearchField == 'time_new' || $SearchField == 'time_up') {
            if ($SearchOperator == '>' || $SearchOperator == '<') {
                $SearchaAtistop = strtotime(strval($SearchaAtistop));
                if ($SearchField == 'time_new') {
                    $SearchField = 'time_new';
                } else if ($SearchField == 'time_up') {
                    $SearchField = 'time_up';
                }
            }
        }
        //  return (json($query));
        $sqldb = mysql::where([
            [$SearchField, $SearchOperator, $SearchaAtistop],
            // ["cls_id", "IN", [1, 150, 129, 128, 123, 124, 154, 120, 121, 122]],
            // ["unit_pack", "=", "B"], //只查小包单位
        ])
            ->order($SearchaSortField, $SearchaSort)
            ->paginate([ //[文档]https://www.kancloud.cn/manual/thinkphp6_0/1037638
                'list_rows'  => $limit, // 每页数量
                'page'  => $current, // 当前页
                // 'query' => $query,//url额外参数
            ]);
        // echo  udb::getLastSql();
        //设置允许输出字段
        $sqldb = $sqldb->visible([
            // 'id', //主键
        ]);
        // 设置需要附加的输出属性
        $sqldb = $sqldb->append([
            'all_sup_addr', //附加字段-货商其它地址
            'all_sup_bank', //附加字段-货商其它银行账号
        ]);
        // 设置数据字段获取器
        $sqldb = $sqldb->withAttr('time_new', function ($value, $data) {
            return  ["val" => $value, "mp" => date('Y-m-d H:i:s', $value), "txt" => "创建时间"];
        });
        $sqldb = $sqldb->withAttr('time_up', function ($value, $data) {
            return  ["val" => $value, "mp" => date('Y-m-d H:i:s', $value), "txt" => "修改时间"];
        });
        for ($i = 0; $i < count($sqldb); $i++) {
            // 货商其它地址
            $sqldb[$i]["all_sup_addr"] =  Db::table('admin__i_sup_addr')->where('sup_dbid', $sqldb[$i]['dbid'])->select();
            // 货商其它银行账号
            $sqldb[$i]["all_sup_bank"] =  Db::table('admin__i_sup_bank')->where('sup_dbid', $sqldb[$i]['dbid'])->select();
        }
        $sqldb2 = $sqldb->toArray();
        //  当前页起始行
        if ($current > 1) {
            $index2 = $limit * $current - $limit + 1;
        } else {
            $index2 = 1;
        }
        // echo "当前总下标：" . $index2;
        for ($x = 0; $x < count($sqldb); $x++) {
            // $li_db[$x]['apitxtx'] = json_decode($li_db[$x]['apitxt'], true); //格式化插入
            // unset($li_db[$x]); //删除数组中的元素
            // ksort($li_db[$x]);//根据关联数组的键，对数组进行升序排列
            $sqldb2["data"][$x]['index1'] = $x + 1;
            $sqldb2["data"][$x]['index2'] = $index2;
            $index2++;
            $reset = null;
            // $reset = Db::table('admin__i_goods_cls')->where('id', $sqldb2["data"][$x]['cls_id'])->find();
            // $sqldb2["data"][$x]['cls_name'] = !empty($reset["name"]) ? $reset["name"] : "!断链分类" . $sqldb2["data"][$x]['cls_id'];
        }
        // dump($sqldb2);
        // dump(count($sqldb));
        // 遍历数据集
        // foreach ($sql_db as $rs) {
        // foreach($age as $x=>$x_value)
        // $rs['event_linkman_card_id']="身份证号码呀---保密了";
        // unset($rs['sys_indexer']);//销毁
        // array_push($tem_data, $rs); //把每个数据集记录插入到数组
        // }
        // unset($tem_data); //销毁
        // array_push()	将一个或多个元素插入数组的末尾（入栈）。
        // array_merge()	把一个或多个数组合并为一个数组。
        // array_merge_recursive()	递归地把一个或多个数组合并为一个数组。
        // $count = count($list);
        //本API说明
        $api_info = [
            '本API说明' => [
                "本API名称" => "货商档案列表",
                "本API版号" => "1.001",
                "本API更新" => "2023-11-25",
                "本API地址" => request()->domain() . request()->baseUrl(),
                "本API当前请求类型" => request()->method(),
                "本API建议请求类型" => "POST",
                "本API要求的请求参数" => [
                    "limit" => "[必填]每页行数:整数", //每页行数:整数
                    "current" => "[必填]当前页:整数", //当前页:整数
                    "SearchField" => "[默认id]搜索字段", //搜索字段
                    "SearchOperator" => "[默认LIKE]搜索查询表达式LIKE/=/>/</<>/IN", //搜索查询表达式LIKE/=/>/</<>/IN
                    "SearchaAtistop" => "[默认'']搜索关键词", //搜索关键词
                    "SearchaSort" => "[默认DESC]搜索排序方式ASE,DESC升降序", //搜索排序方式ASE,DESC升降序
                    "SearchaSortField" => "[默认id]排序字段", //排序字段
                    "additional" => [
                        "use" => "[必填]启用额外查询方法:整数-0否-1是", //启用额外查询方法:整数-0否-1是
                        "SearchField" => "[默认id]搜索字段", //搜索字段
                        "SearchOperator" => "[默认LIKE]搜索查询表达式LIKE/=/>/</<>/IN", //搜索查询表达式LIKE/=/>/</<>/IN
                        "SearchaAtistop" => "[默认'']搜索关键词", //搜索关键词
                    ],
                    "additional启用额外查询方法-说明" => "主要用来不改变默认条件选择框的值的另一种隐匿查询方法。如直接点击按分类查询,多个字段查询等。", //排序字段
                ],
                "本API简介" => "",
                "请求回调字段说明" => [
                    "code" => "状态码:整数(200=正常/)",
                    "msg" => "状态信息:字符串",
                    "query" => "您发出的请求参数",
                    "分页与数据集说明" => [
                        "db" => [
                            "total" => "数据总行数:整数",
                            "per_page" => "每页数量:整数",
                            "current_page" => "当前页码:整数",
                            "last_page" => "最后一页/总页数:整数",
                            "data" => "数据集:数组",
                        ],
                        "db.data 数据集字段说明"  => [
                            "id" =>  "主键:整数", //主键
                            "time_new" =>  "创建时间:对象{val,mp,txt}", //创建时间
                            "time_up" =>  "修改时间:对象{val,mp,txt}", //修改时间
                            "db_json" => "静态JSON", //静态JSON
                            "dbid" =>  "数据编号.唯一:字符串", //数据编号.唯一
                            "name" =>  "货商名称:字符串", //货商名称
                            "principal" =>  "默认负责人:字符串", //默认负责人
                            "tel" =>  "默认电话:字符串", //默认电话
                            "mobile" =>  "默认手机:字符串", //默认手机
                            "fax" =>  "默认传真:字符串", //默认传真
                            "email" =>  "默认电子邮箱:字符串", //默认电子邮箱
                            "bank_acctno" =>  "默认开户银行:字符串", //默认开户银行
                            "bank_no" =>  "默认银行账号:字符串", //默认银行账号
                            "addr" =>  "默认地址:字符串", //默认地址
                            "memo" =>  "备注:字符串", //备注
                            "index1" =>  "附加字段-行号", //附加字段-行号
                            "all_sup_addr" => "附加字段-货商其它地址", //附加字段-货商其它地址
                            "all_sup_bank" => "附加字段-货商其它银行账号", //附加字段-货商其它银行账号
                            "all_sup_addr 数据集字段说明"  => [
                                "id" => "主键:整数",
                                "sup_dbid" => "归属货商数据编号",
                                "name" => "名称",
                                "principal" => "负责人",
                                "tel" => "联系电话",
                                "mobile" => "手机",
                                "fax" => "传真",
                                "email" => "默认电子邮箱",
                                "addr" => "地址",
                                "memo" => "备注"
                            ],
                            "all_sup_bank 数据集字段说明"  => [
                                "id" => "主键:整数",
                                "sup_dbid" => "归属货商数据编号",
                                "name" => "名称",
                                "bank_acctno" => "开户银行",
                                "bank_no" => "银行账号",
                                "memo" => "备注"
                            ],
                        ]
                    ]
                ]
            ],
        ];
        $apidb = [
            'api_info' => $api_info, //API信息
            'code' => 200, //状态码
            'msg' => "查询成功！", //状态信息
            'query' => $query, //请求参数
            // '_header' => request()->header('token'),
            // '_cache' => cache('userdb_' . request()->header('token')),
            'db' => $sqldb2,
            "script_time" => date('Y-m-d H:i:s', time()), //脚本时间
        ];
        // unset($sqldb2); //销毁
        return (json($apidb));
    }
}
